﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête friends timeline</summary>
    /// <see cref="friends_timeline"></see>
    [DataContract]
    public class family_timeline
    {
        /// <summary>non utilisé</summary>
        //[DataMember]
        //public bool favorited { get; set; }

        /// <summary>texte du message</summary>
        [DataMember]
        public string text { get; set; }

        /// <summary>non utilisé</summary>
        //[DataMember]
        //public int in_reply_to_user_id { get; set; }

        /// <summary>date du message au format suivant : Tue Sep 16 11:38:39 +0200 2008 </summary>
        /// <remarks>en PHP : date(D M j H:i:s O Y</remarks>
        [DataMember]
        public string created_at { get; set; }

        /// <summary>date du message au format ISO 8601</summary>
        [DataMember]
        public string created_at_iso8601 { get; set; }

        /// <summary>non utilisé</summary>
        //[DataMember]
        //public bool truncated { get; set; }

        /// <summary>url du site de famille</summary>
        [DataMember]
        public string source { get; set; }

        /// <summary>id du message</summary>
        [DataMember]
        public int id { get; set; }

        /// <summary>utilisateur</summary>        
        [DataMember]
        public user user { get; set; }

        /// <summary>site</summary>        
        [DataMember]
        public site site { get; set; }

    }

    public delegate void FamilyTimelineEventHandler(object sender, FamilyTimelineEventArgs e);
    public class FamilyTimelineEventArgs : EventArgs
    {
        public FamilyTimelineEventArgs(ObservableCollection<family_timeline> ft)
        {
            _result = ft;
        }

        private ObservableCollection<family_timeline> _result;
        public ObservableCollection<family_timeline> result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event FamilyTimelineEventHandler FamilyTimelineCompleted;

        #region Family Timeline
        /// <summary>Permet de récupérer la liste des 20 derniers messages publics d’un site de famille de l’utilisateur authentifié.</summary>
        /// <c>http://apitxt.hellotipi.com/statuses/family_timeline.format</c>
        /// <remarks>1 paramètre optionnel par requête</remarks>
        /// <param name="site">nom du site de famille</param>
        /// <param name="since">limite les résultats aux messages postés après cette date ; le même comprotement peut être obtenu en spécifiant l’entête HTTP If-Modified-Since dans la requête. Date au format RFC 2822</param>
        /// <param name="since_id">renvoie les messages postés après le message since_id</param>
        /// <param name="count">nombre de messages à renvoyer, 20 par défaut, 200 max</param>
        /// <param name="page">numéro de la page demandée</param>
        /// <returns>La liste <paramref name="family_timeline"/> des message publics du site <paramref name="site"/>.</returns>
        public void FamilyTimeline(string site, DateTime? since = null, int? since_id = null, int? count = null, int? page = null)
        {
            base.APICallCompleted += new APICallCompletedEventHandler(FamilyTimelineCallCompleted);

            var querystring = new List<KeyValuePair<string, string>> { 
                new KeyValuePair<string, string>("site", site),
                new KeyValuePair<string, string>("source", this.appSource)
            };

            if (since != null)
                querystring.Add(new KeyValuePair<string, string>("since", since.ToString()));

            else if (since_id != null)
                querystring.Add(new KeyValuePair<string, string>("since_id", since_id.ToString()));

            else if (count != null)
                querystring.Add(new KeyValuePair<string, string>("count", count.ToString()));

            else if (page != null)
                querystring.Add(new KeyValuePair<string, string>("page", since.ToString()));

            GetUrlApi("statuses/family_timeline.json", querystring);
        }

        void FamilyTimelineCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                    FamilyTimelineCompleted.Invoke(sender, new FamilyTimelineEventArgs(serializeTools.Deserialize<ObservableCollection<family_timeline>>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(FamilyTimelineCallCompleted);
            }

        }

        #endregion
    }

}

